#include <stdio.h>
#include <stdlib.h>

int size;

struct node
{
	int data;
	struct node *next;
};

typedef struct node node_t;

typedef void (*callback)(node_t *data);

node_t *init(node_t *rv, int data)
{
	node_t *newNode = NULL;
	newNode = malloc(sizeof(node_t));
	if(newNode == NULL)
	{
		printf("Error creating new node");
		return NULL;
	}
	newNode->data = data;
	newNode->next = rv;
	
	return newNode;
}

node_t *addNode(node_t *rv, int n)
{
	if(rv == NULL)
		return NULL;
		
	node_t *cursor = rv;
	while(cursor->next != NULL)
		cursor = cursor->next;
	
	node_t *newNode = init(NULL, n);
	cursor->next = newNode;
	
	return rv;
}

void display(node_t *head)
{
	if(head != NULL)
		printf("data: %d\n", head->data);
}

int counter(node_t *head)
{
	node_t *cursor = head;
	int c = 0;
	while(cursor != NULL)
	{
		c++;
		cursor = cursor->next;
	}
	return c;
}

void traverse(node_t *head, callback f)
{
	node_t *cursor = head;
	while(cursor != NULL)
	{
		f(cursor);
		cursor = cursor->next;
	}
}

void main(void)
{
	node_t *Output = NULL;
	callback disp = display;
	
	Output = addNode(Output,0);
	traverse(Output, disp);
	
	printf("step1\n");
	Output = addNode(Output,5);
	traverse(Output, disp);
	
	printf("step2\n");
	Output = addNode(Output,1);
	traverse(Output, disp);
	
	printf("step3\n");
	Output = addNode(Output,0);
	traverse(Output, disp);
}

